package cn.skyquant.quant4j.service.boot.web.controller;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.enums.BorkerType;
import cn.skyquant.quant4j.api.enums.GoodsType;
import cn.skyquant.quant4j.api.goods.GoodsDTO;
import cn.skyquant.quant4j.api.goods.GoodsService;
import cn.skyquant.quant4j.sdk.util.string.StringUtils;
import cn.skyquant.quant4j.service.boot.model.authority.RoleAuthority;
import cn.skyquant.quant4j.service.boot.model.goods.Goods;
import cn.skyquant.quant4j.service.boot.model.goods.GoodsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/goods")
@RoleAuthority(roleNames = {RoleAuthority.administrator,RoleAuthority.client})
public class GoodsController {
    @Autowired
    GoodsRepository goodsRepository ;

    @Autowired
    GoodsService goodsService;

    @RequestMapping(value = "",method = RequestMethod.GET)
    public ResultDTO<GoodsDTO> get(
            @RequestParam(value = "symbol",required = true) String symbol,
            @RequestParam(value = "borkerType",required = true) BorkerType borkerType){
        return goodsService.get(symbol,borkerType);
    }


    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<List<Goods>> list(
            @RequestParam(value = "symbol",required = false) String symbol,
            @RequestParam(value = "borkerType",required = true) BorkerType borkerType
    ){
        List<Goods> list;
        if(StringUtils.isEmpty(symbol)){
            list = goodsRepository.findByBorkerType(borkerType);
        }else {
            list = goodsRepository.findBySymbolAndBorkerType(symbol.toUpperCase(),borkerType);
        }
        return ResultDTO.success(list);
    }

    @Transactional
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public ResultDTO<Void> delete(
            @PathVariable(value = "id",required = true) Integer id
    ){
        return goodsService.deleteGoods(id);
    }



    @Transactional
    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Void> saveOrUpdate(
            @RequestParam(value = "symbol",required = true) String symbol,
            @RequestParam(value = "borkerType",required = true) BorkerType borkerType,
            @RequestParam(value = "type",required = true) GoodsType type
    ){
        if(symbol==null || borkerType==null || type==null){
            return ResultDTO.error;
        }
        Optional<Goods> goodsOptional = goodsRepository.getBySymbolAndBorkerType(symbol,borkerType);
        if(goodsOptional.isPresent()){
            Goods goods = goodsOptional.get();
            goodsService.updateGoods(goods.id,type,goods.avgSpread);
        }else {
            goodsService.saveGoods(symbol,borkerType,type);
        }
        return ResultDTO.success;
    }
}
